struct DXToVertex
{
   float3 Position	:POSITION;
   float3 Normal	:NORMAL;
   float2 Texture0	:TEXCOORD0;
   float4 Blender	:TEXCOORD1;
};

struct VertexToPixel
{
	float4 Position     :POSITION;
	float3 Normal	:TEXCOORD0;
	float2 Texture0	:TEXCOORD1;    
	float4 Pos		:TEXCOORD2;
	float4 Blender      :TEXCOORD3;
	float4 Color	:COLOR0; 
	float  Depth	:TEXCOORD4;   
	
};

Texture grasstex;
Texture sandtex;
Texture rocktex;
Texture snowtex;

sampler grasstexSampler = sampler_state 
{
	texture = <grasstex> ; 
	magfilter = LINEAR; 
	minfilter = LINEAR; 
	mipfilter=LINEAR; 
	//AddressU = mirror; 
	//AddressV = mirror;
};

sampler sandtexSampler = sampler_state 
{
	texture = <sandtex> ; 
	magfilter = LINEAR; 
	minfilter = LINEAR; 
	mipfilter=LINEAR; 
	//AddressU = mirror; 
	//AddressV = mirror;
};
sampler rocktexSampler = sampler_state 
{
	texture = <rocktex> ; 
	magfilter = LINEAR; 
	minfilter = LINEAR; 
	mipfilter=LINEAR; 
	AddressU = mirror; 
	AddressV = mirror;
};
sampler snowtexSampler = sampler_state 
{
	texture = <snowtex> ; 
	magfilter = LINEAR; 
	minfilter = LINEAR; 
	mipfilter=LINEAR; 
	//AddressU = mirror; 
	//AddressV = mirror;
};

float4x4 xWorldViewProj;

float4 xMaterialAmbient;
float4 xMaterialDiffuse;
float4 xMaterialSpecular;
float4 xMaterialEmissive;

float4 xLight;
float4 xLightPos;
float4 xLightAmbient;
float4 xLightDiffuse;
float4 xLightSpecular;

VertexToPixel SimplestVertexShader( DXToVertex In)
{
	VertexToPixel Output = (VertexToPixel)0;
	//In.Normal.w = 1.0f;    
	//xMaterialEmissive = float4(-0.5f,-0.5f,-0.5f,1.0f); //wyrownuje kolor ... nie wiem dlaczego tak !     
	
	Output.Normal = In.Normal.xyz;
	Output.Position =  mul(float4(In.Position.xyz,1.0f), xWorldViewProj);
	Output.Pos = float4(In.Position.xyz,1.0f);
	Output.Texture0 = In.Texture0; 
	Output.Blender = In.Blender;
	Output.Depth = Output.Position.z/Output.Position.w;   
	
	return Output;    
}

float4 SimplestPixelShader(VertexToPixel In) :COLOR0
{
	float blendEnd = 0.98f;
	float blendWidth = 0.01f;
	float depthBlender = clamp((In.Depth-blendEnd)/blendWidth, 0, 1);
	
	float4 PointLight = normalize(xLightPos-In.Pos);
	float4 xTotalLightDiffuse =  xLightDiffuse*dot(float4(In.Normal.xyz,1),PointLight);
	
	float2 newTexCoords = In.Texture0*4;
	
	float4 far;
	float4 near = tex2D(grasstexSampler, newTexCoords )*In.Blender.y;
	near += far = tex2D(sandtexSampler, newTexCoords )*In.Blender.x;
	near += tex2D(rocktexSampler, newTexCoords )*In.Blender.z;
	near += tex2D(snowtexSampler, newTexCoords )*In.Blender.w;

	far += tex2D(grasstexSampler, In.Texture0)*In.Blender.y;
	//far += tex2D(sandtexSampler, newTexCoords )*In.Blender.x;
	far += tex2D(rocktexSampler, In.Texture0)*In.Blender.z;
	far += tex2D(snowtexSampler, In.Texture0)*In.Blender.w;

	float4 ColorPS = lerp(near, far, depthBlender);

	ColorPS *= xTotalLightDiffuse * xMaterialDiffuse *1.0f;
	
	//return In.Blender;
	return ColorPS;

}

technique Simple
{
	pass Pass0
	{
		AlphaBlendEnable = false;
		ZWriteEnable = true;
		CullMode = none;

		VertexShader = compile vs_1_1 SimplestVertexShader();
		PixelShader = compile ps_2_0 SimplestPixelShader();
	}
}